package com.gupt.weekly.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.gupt.weekly.dao.PlanMapper;
import com.gupt.weekly.domain.Plan;
import com.gupt.weekly.service.PlanService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.time.LocalDateTime;
import java.util.List;

/**
 * @author linzizun
 * @version 1.0
 * @date 2021/03/12 16:07
 */
@Service
public class PlanServiceImpl implements PlanService {
    @Resource
    PlanMapper planMapper;

    @Override
    public int insert(Plan plan) {
        return planMapper.insert(plan);
    }

    @Override
    public Plan selectById(String id) {
        return planMapper.selectById(id);
    }

    @Override
    public List<Plan> selectList(Plan plan) {
        QueryWrapper<Plan> queryWrapper = new QueryWrapper<>();
        queryWrapper.like("title", plan.getTitle())
                .eq("user_id", plan.getUser_id())
                .eq("has_release", plan.getHas_release());
        return planMapper.selectList(queryWrapper);
    }

    @Override
    public List<Plan> selectNotStartList(Plan plan) {
        QueryWrapper<Plan> queryWrapper = new QueryWrapper<>();
        queryWrapper.like("title", plan.getTitle())
                .eq("user_id", plan.getUser_id())
                .eq("has_release", plan.getHas_release())
                .gt("start_time", LocalDateTime.now());
        return planMapper.selectList(queryWrapper);
    }

    @Override
    public List<Plan> selectNotClosedList(Plan plan) {
        QueryWrapper<Plan> queryWrapper = new QueryWrapper<>();
        queryWrapper.like("title", plan.getTitle())
                .eq("user_id", plan.getUser_id())
                .eq("has_release", plan.getHas_release())
                .gt("deadline", LocalDateTime.now());
        return planMapper.selectList(queryWrapper);
    }

    @Override
    public List<Plan> selectOngoingList(Plan plan) {
        QueryWrapper<Plan> queryWrapper = new QueryWrapper<>();
        queryWrapper.like("title", plan.getTitle())
                .eq("user_id", plan.getUser_id())
                .eq("has_release", plan.getHas_release())
                .lt("start_time", LocalDateTime.now())
                .gt("deadline", LocalDateTime.now());
        return planMapper.selectList(queryWrapper);
    }

    @Override
    public List<Plan> selectHasClosedList(Plan plan) {
        QueryWrapper<Plan> queryWrapper = new QueryWrapper<>();
        queryWrapper.like("title", plan.getTitle())
                .eq("user_id", plan.getUser_id())
                .eq("has_release", plan.getHas_release())
                .lt("deadline", LocalDateTime.now());
        return planMapper.selectList(queryWrapper);
    }

    @Override
    public int updateById(Plan plan) {
        return planMapper.updateById(plan);
    }
}
